Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if page methods are missing await (missing-playwright-await) #310

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

camchenry
Copy link
Contributor

@camchenry camchenry commented Aug 20, 2024

This adds support for checking if Page methods are properly awaited, as part of the missing-playwright-await rule.

I've intentionally skipped the waitForEvent, waitForRequest, and waitForResponse methods, as I believe these should be covered by a different lint rule or PR, since it is more complex: #199

@@ -247,6 +247,21 @@ runRuleTester('missing-playwright-await', rule, {
},
},
},
// Page methods
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would love to hear more ideas on test cases here. It's a bit simpler to check page methods than the test/except cases, since it's not a chained call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add test cases to assert myPage.goto, frame.goto, myFrame.goto, etc.

@@ -57,6 +57,76 @@ const playwrightTestMatchers = [
'toBeInViewport',
]

const pageMethods = new Set([
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check these 🙏 I tried to alphabetically sort them and make sure I included all methods that return Promise and not any that don't, but I could have made a mistake here.

@@ -116,10 +116,17 @@ export function dig(node: ESTree.Node, identifier: string | RegExp): boolean {
: false
}

export function isPage(node: ESTree.CallExpression) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of dubious on whether this function is a good idea or not, since you have to check if node.callee is a MemberExpression anyway to get the type checking to work properly. Open to thoughts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about instead accepting a MemberExpression and then doing isPage(node.callee).

Comment on lines +387 to +394
{ code: `await page.goto('https://example.com')` },
{ code: `await page.title()` },
// Other page methods are ignored
{ code: `page.frames()` },
// Other methods with the same name are ignored
{ code: `randomObject.title()` },
// Does not need to be awaited when returned
{ code: `() => { return page.content() }` },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a few tests for this, e.g. this.page.goto()

@@ -247,6 +247,21 @@ runRuleTester('missing-playwright-await', rule, {
},
},
},
// Page methods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add test cases to assert myPage.goto, frame.goto, myFrame.goto, etc.

@@ -116,10 +116,17 @@ export function dig(node: ESTree.Node, identifier: string | RegExp): boolean {
: false
}

export function isPage(node: ESTree.CallExpression) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about instead accepting a MemberExpression and then doing isPage(node.callee).

messageId: 'page',
node: node.callee,
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and return at the end of this if statement, if it is a page method, no need to run parseFnCall since it won't be a expect or test function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants